#ifndef TIMER_LIST_H
#define TIMER_LIST_H


#include<netinet/in.h>
#include<time.h>
#include<stdio.h>
#define BUFFER_SIZE 64

class util_timer
{
public:
    util_timer(int sock):socket(sock),prev(NULL),next(NULL)
    {
        time_t cur=time(NULL);
        this->expire=cur+15;
    }

public:
    time_t expire;
    int socket;
    util_timer* prev;
    util_timer* next;
};

class sort_timer_lst
{
public:
    sort_timer_lst():head(NULL){}

    void add_timer(int sock)
    {
        util_timer *timer=new util_timer(sock);
        if(!timer)
        {
            return;
        }
        if(!head)
        {
            head=timer;
            return;
        }

        if(timer->expire<head->expire)
        {
            timer->next=head;
            head=timer;
            return;
        }
        add_timer(timer,head);
    }

    void del_timer(int sock)
    {
        util_timer *timer=head;
        while(timer)
        {
            if(timer->socket==sock)
            {
                break;
            }
            timer=timer->next;
        }
        if(head==timer)
        {
            head=NULL;
            delete timer;
            return;
        }
        timer->prev->next=timer->next;
        delete timer;
    }

    void adjust_timer(int sock)
    {
        util_timer *timer=head;
        while(timer)
        {
            if(timer->socket==sock)
            {
                break;
            }
            timer=timer->next;
        }
        if(!timer)
        {
            return;
        }
        timer->expire+=15;
        util_timer *tmp=timer=timer->next; 
        while(tmp)
        {
            if(!tmp||(timer->expire<tmp->expire))
            {
                return;
            }
            timer->prev->next=tmp;
            timer->next=tmp->next;
            tmp->next=timer;
            tmp=timer->next;
        }
    }


    util_timer* tick()
    {
        if((!head))
        {
            return NULL;
        }
        printf("timer tick\n");
        time_t cur=time(NULL);
        if(cur<head->expire)
        {
            return NULL;
        }
        util_timer* tmp=head->next;
        util_timer* retur=head;
        while(tmp)
        {
            if(cur<tmp->expire)
            {
                tmp->prev->next=NULL;
                head=tmp;
                break;
            }
            head=tmp->next;
            tmp=head;
        }
        return retur;
    }

    util_timer* get_head()
    {
        return head;
    }

private:
    void add_timer(util_timer* timer,util_timer*lst_head)
    {
        util_timer* prev=lst_head;
        util_timer* tmp=prev->next;

        while(tmp)
        {
            if(timer->expire<tmp->expire)
            {
                prev->next=timer;
                timer->prev=prev;
                timer->next=tmp;
                tmp->prev=timer;
                break;
            }
            prev=tmp;
            tmp=tmp->next;
        }

        if(!tmp)
        {
            prev->next=timer;
            timer->prev=prev;
            timer->next=NULL;
        }
    }


public:
    util_timer* head;
};



#endif